from django.db import models
from django.contrib.auth.models import User


class Category(models.Model):
    """商品分类模型 - 用于演示简单的CRUD操作"""
    name = models.CharField(max_length=100, verbose_name='分类名称')
    description = models.TextField(blank=True, verbose_name='分类描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '商品分类'
        verbose_name_plural = '商品分类'
        ordering = ['-created_at']

    def __str__(self):
        return self.name


class Product(models.Model):
    """产品模型 - 用于演示复杂的关联和序列化"""
    STATUS_CHOICES = [
        ('active', '在售'),
        ('inactive', '下架'),
        ('draft', '草稿'),
    ]

    name = models.CharField(max_length=200, verbose_name='产品名称')
    description = models.TextField(verbose_name='产品描述')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products', verbose_name='分类')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active', verbose_name='状态')
    stock = models.PositiveIntegerField(default=0, verbose_name='库存')
    image = models.ImageField(upload_to='products/%Y/%m/', blank=True, null=True, verbose_name='产品图片', help_text='支持jpg, png, gif等格式')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_products', verbose_name='创建者')

    class Meta:
        verbose_name = '产品'
        verbose_name_plural = '产品'
        ordering = ['-created_at']

    def __str__(self):
        return self.name


class Review(models.Model):
    """评论模型 - 用于演示嵌套序列化和权限控制"""
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='reviews', verbose_name='产品')
    reviewer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reviews', verbose_name='评论者')
    rating = models.PositiveSmallIntegerField(choices=[(i, i) for i in range(1, 6)], verbose_name='评分')
    comment = models.TextField(verbose_name='评论内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '产品评论'
        verbose_name_plural = '产品评论'
        ordering = ['-created_at']
        unique_together = ['product', 'reviewer']  # 每个用户对同一产品只能评论一次

    def __str__(self):
        return f'{self.reviewer.username} - {self.product.name} ({self.rating}星)'


class Tag(models.Model):
    """标签模型 - 用于演示多对多关系"""
    name = models.CharField(max_length=50, unique=True, verbose_name='标签名称')
    color = models.CharField(max_length=7, default='#000000', verbose_name='标签颜色')

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = '标签'

    def __str__(self):
        return self.name


class ProductTag(models.Model):
    """产品标签关联模型 - 用于演示通过中间模型的多对多关系"""
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='产品')
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE, verbose_name='标签')
    added_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='添加者')
    added_at = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        verbose_name = '产品标签'
        verbose_name_plural = '产品标签'
        unique_together = ['product', 'tag']

    def __str__(self):
        return f'{self.product.name} - {self.tag.name}'
